﻿@inject IStringLocalizer<LocatorsNormal> Localizer
@inject WebClientService ClientService
@inject IIPLocatorProvider IPLocator

<div>
    <div class="mb-2">@Localizer["LocatorsNormalTipsTitle"]</div>
    <div><code>112.224.74.239</code> @Localizer["LocatorsNormalTips1"]</div>
    <div class="mb-3"><code>183.160.236.53</code> @Localizer["LocatorsNormalTips2"]</div>
    <div class="row g-3 form-inline">
        <div class="col-12 col-sm-6">
            <BootstrapInput @bind-Value="Ip" DisplayText="@Localizer["LocatorsNormalInputText"]" ShowLabel="true" />
        </div>
        <div class="col-12 col-sm-6">
            <Display Value="Location" DisplayText="@Localizer["LocatorsNormalDisplayText"]" ShowLabel="true" />
        </div>
        <div class="col-12">
            <Button Icon="fa-solid fa-location-arrow" Text="@Localizer["LocatorsNormalButtonText"]" OnClick="OnClick" />
        </div>
    </div>
</div>

@code {
    private string? Ip { get; set; }

    private string? Location { get; set; }

    /// <summary>
    /// OnAfterRenderAsync
    /// </summary>
    /// <param name="firstRender"></param>
    /// <returns></returns>
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender)
        {
            var clientInfo = await ClientService.GetClientInfo();
            Ip = clientInfo.Ip;
            StateHasChanged();
        }
    }

    private async Task OnClick()
    {
        if (!string.IsNullOrEmpty(Ip))
        {
            Location = await IPLocator.Locate(Ip);
        }
    }
}
